//
//  CNBBSNetManager.m
//  CsdnPlus
//
//  Created by 孟瑞瑞 on 2018/5/29.
//  Copyright © 2018年 CSDN. All rights reserved.
//

#import "CNBBSNetManager.h"
#import <CNBaseNetworkFramework/CNBaseNetworkFramework.h>
#import <YYModel/YYModel.h>
#import <CNBaseViewTool/CNViewConst.h>
#import <CNBaseViewTool/CSDNUser.h>

#pragma mark - 论坛
/*-------------------------------- 论坛 -----------------------------------*/
#pragma mark - 技术区，生活区热帖

NSString *const kBBSHotTopicsPath = @"%@/cms-app/v1/bbs/new_hot_topics";

#pragma mark - 版块列表

NSString *const kBBSSectionListPath = @"%@/cms-app/v1/bbs/may_login/forums_tree";

#pragma mark - 感兴趣版块

NSString *const kBBSFavoritePath = @"%@/cms-app/v1/bbs/login/add_favorite_topics";

#pragma mark - 不敢兴趣版块

NSString *const kBBSUnfavoritePath = @"%@/cms-app/v1/bbs/login/cancel_favorite_topics";

#pragma mark - 上传图片
NSString *const kBBSUploadPicPath = @"%@/cms-app/v1/bbs/login/up_picture";

#pragma mark - 发帖
NSString *const kBBSPostedPath = @"%@/cms-app/v1/bbs/login/creat_topic";

#pragma mark - 帖子积分
NSString *const kBBSScorePath = @"%@/cms-app/v1/bbs/login/score";

#pragma mark - 论坛详情

NSString *const kBBSDetailPath = @"%@/cms-app/v1/bbs/get_topic_details";

#pragma mark - 论坛回复列表

NSString *const kBBSReplyListPath = @"%@/cms-app/v1/bbs/list_reply_topic";

#pragma mark - 论坛回复帖子

NSString *const kBBSCreatePostPath = @"%@/cms-app/v1/bbs/login/create_reply_topic";

#pragma mark - 论坛获取引用内容

NSString *const kBBSQuotePath = @"%@/cms-app/v1/bbs/get_quote_reply_topic";

#pragma mark - 论坛点赞

NSString *const kBBSDiggPath = @"%@/cms-app/v1/bbs/login/create_digg";

#pragma mark - 论坛举报

NSString *const KBBSReportPath = @"%@/cms-app/v1/bbs/login/create_report";

#pragma mark - 任务数
NSString *const kBBSTaskCountPath = @"%@/cms-app/v2/home_page/login/score_task_count";


NSString *const kCsdnSelectKVSPath = @"%@/cms-app/v1/config/read_ios_by_keys";

#pragma mark - 收藏状态
NSString *const kCheckCollectionPath = @"%@/cms-app/v1/blog_details/login/get_favorites";

@implementation CNBBSNetManager

/**
 获取技术区非技术（热帖）列表
 
 @param type 类型
 @param page 页数
 @return 网络请求任务
 */
+(id)getBBSHotTopicListWithTocipType:(NSString *)tocipType AndPage:(NSInteger)page completionHandle:(void (^)(id, NSError *))completionHandle
{
    NSMutableDictionary *params = [[NSMutableDictionary alloc]initWithDictionary:
                                   @{
                                     @"page":@(page),
                                     @"topicType":tocipType,
                                   }];
    
    
    BADataEntity *entiy = [[BADataEntity alloc]init];
    entiy.urlString = [NSString stringWithFormat:kBBSHotTopicsPath,[CNChangeHostManager sharedChangeHostObject].kCNALiNewHost];
    entiy.parameters = params;
    entiy.needCache = NO;
    
    return [self ba_request_GETWithEntity:entiy successBlock:^(id response) {
        completionHandle([CNBBSListModel yy_modelWithJSON:response],nil);
    } failureBlock:^(NSError *error) {
        completionHandle(nil,error);
    } progressBlock:nil];
}

/**
 *  获取BBS版块列表
 *
 *  @param completionHandle  解析回调
 *
 *  @return 网络请求任务
 */
+ (id)getBBSForumListCompletionHandle:(void (^)(id, NSError *))completionHandle
{
    NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
    
    BADataEntity *entiy = [[BADataEntity alloc]init];
    entiy.urlString = [NSString stringWithFormat:kBBSSectionListPath,[CNChangeHostManager sharedChangeHostObject].kCNALiNewHost];
    entiy.parameters = params;
    entiy.needCache = NO;
    
    return [self ba_request_GETWithEntity:entiy successBlock:^(id response) {
        completionHandle([CNForumListModel yy_modelWithJSON:response],nil);
    } failureBlock:^(NSError *error) {
        completionHandle(nil,error);
    } progressBlock:nil];
}

/**
 编辑论坛版块是否感兴趣
 
 @param forumId 类型
 @param isFavorite 是否感兴趣
 @return 网络请求任务
 */
+(id)getBBSFavoriteWithForumId:(NSString *)forumId AndIsFavorite:(BOOL)isFavorite completionHandle:(void (^)(id, NSError *))completionHandle
{
    NSDictionary *params = @{
                             @"forumId":forumId,
                             };
    BADataEntity *entiy = [[BADataEntity alloc]init];
    if (!isFavorite)
    {
        entiy.urlString = [NSString stringWithFormat:kBBSFavoritePath,[CNChangeHostManager sharedChangeHostObject].kCNALiNewHost];
    }
    else
    {
        entiy.urlString = [NSString stringWithFormat:kBBSUnfavoritePath,[CNChangeHostManager sharedChangeHostObject].kCNALiNewHost];
    }
    
    entiy.parameters = params;
    
    return [self ba_request_POSTWithEntity:entiy successBlock:^(id response) {
        completionHandle([CNBBSFavoriteModel yy_modelWithJSON:response],nil);
    } failureBlock:^(NSError *error) {
        completionHandle(nil,error);
    } progressBlock:nil];
}

/**
 发帖
 
 @param title 标题
 @param body  内容
 @param forumId 类型
 @param point 积分
 @return 网络请求任务
 */
+(id)getBBSPostedWithTitle:(NSString *)title AndBody:(NSString *)body AndForumId:(NSString *)forumId AndPoint:(NSString *)point completionHandle:(void (^)(id, NSError *))completionHandle
{
    NSString *sign = [[CNViewConst stringFromMD5WithString:[NSString stringWithFormat:@"title=%@&forum_id=%@&key=%@",title,forumId,[CNViewConst getLoginCSDNMD5Key]]] uppercaseString]; 
    
    NSDictionary *params = @{
                             @"forumId":forumId,
                             @"body":body,
                             @"title":title,
                             @"point":point,
                             @"sign":sign,
                             };
    BADataEntity *entiy = [[BADataEntity alloc]init];
    
    entiy.urlString = [NSString stringWithFormat:kBBSPostedPath,[CNChangeHostManager sharedChangeHostObject].kCNALiNewHost];
    entiy.parameters = params;
    
    return [self ba_request_POSTWithEntity:entiy successBlock:^(id response) {
        completionHandle([CNBBSPostedModel yy_modelWithJSON:response],nil);
    } failureBlock:^(NSError *error) {
        completionHandle(nil,error);
    } progressBlock:nil];
}

/**
 *  获取BBS积分
 *
 *  @param completionHandle  解析回调
 *
 *  @return 网络请求任务
 */
+ (id)getBBSScoreCompletionHandle:(void(^)(id model, NSError *error))completionHandle
{
    BADataEntity *entiy = [[BADataEntity alloc]init];
    entiy.urlString = [NSString stringWithFormat:kBBSScorePath,[CNChangeHostManager sharedChangeHostObject].kCNALiNewHost];
    entiy.needCache = NO;
    
    return [self ba_request_GETWithEntity:entiy successBlock:^(id response) {
        completionHandle([CNBBSPostedImageModel yy_modelWithJSON:response],nil);
    } failureBlock:^(NSError *error) {
        completionHandle(nil,error);
    } progressBlock:nil];
}

/**
 上传图片
 
 @param imageArray 图片数组
 @param fileNames  图片名
 @param imageType  图片类型
 @return 网络请求任务
 */
+(id)getBBSUpLoadPicWithImageArray:(NSArray *)imageArray AndImageTypes:(NSArray *)imageTypes completionHandle:(void (^)(id, NSError *))completionHandle
{
    
    BAImageDataEntity *imageEntity = [[BAImageDataEntity alloc]init];
    imageEntity.urlString = [NSString stringWithFormat:kBBSUploadPicPath,[CNChangeHostManager sharedChangeHostObject].kCNALiNewHost];
    imageEntity.parameters = nil;
    imageEntity.needCache = NO;
    imageEntity.imageDataArray = imageArray;
    imageEntity.imageTypes = imageTypes;
    imageEntity.fileNames = @[@"markdown_file"];
    imageEntity.name = @"file";
    
    return [self ba_uploadImageDataWithEntity:imageEntity successBlock:^(id response) {
        completionHandle([CNBBSPostedImageModel yy_modelWithJSON:response],nil);
    } failurBlock:^(NSError *error) {
        completionHandle(nil,error);
    } progressBlock:nil];
}

/**
 论坛详情数据
 
 @param topicId 论坛id
 
 @return 网络请求任务
 */
+(id)getBBSDetailWithTopicId:(NSString *)topicId completionHandle:(void (^)(id, NSError *))completionHandle
{
    NSDictionary *params = @{
                             @"bodyFormat":@"1",
                             @"topicId":topicId,
                             };
    
    BADataEntity *entiy = [[BADataEntity alloc]init];
    entiy.urlString = [NSString stringWithFormat:kBBSDetailPath,[CNChangeHostManager sharedChangeHostObject].kCNALiNewHost];
    entiy.parameters = params;
    entiy.needCache = NO;
    
    return [self ba_request_GETWithEntity:entiy successBlock:^(id response) {
        completionHandle([CNBBSDetailModel yy_modelWithJSON:response],nil);
    } failureBlock:^(NSError *error) {
        completionHandle(nil,error);
    } progressBlock:nil];
}

/**
 获取BBS回复列表
 
 @param tocipId 论坛id
 @param page 页数
 @return 网络请求任务
 */
+(id)getBBSReplyListWithTocipId:(NSString *)tocipId AndPage:(NSInteger)page IsInverted:(BOOL)isInverted completionHandle:(void (^)(id, NSError *))completionHandle
{
    NSMutableDictionary *params = [[NSMutableDictionary alloc]initWithDictionary:
                                   @{
                                     @"page":@(page),
                                     @"size":@"10",
                                     @"topicId":tocipId,
                                     }];
    if (isInverted)
    {
        [params setObject:@"reverse" forKey:@"type"];
    }
    
    BADataEntity *entiy = [[BADataEntity alloc]init];
    entiy.urlString = [NSString stringWithFormat:kBBSReplyListPath,[CNChangeHostManager sharedChangeHostObject].kCNALiNewHost];
    entiy.parameters = params;
    entiy.needCache = NO;
    
    return [self ba_request_GETWithEntity:entiy successBlock:^(id response) {
        completionHandle([CNBBSReplyListModel yy_modelWithJSON:response],nil);
    } failureBlock:^(NSError *error) {
        completionHandle(nil,error);
    } progressBlock:nil];
}

/**
 回复论坛
 
 @param topicId 论坛id
 
 @param body 内容
 
 @return 网络请求任务
 */
+(id)getBBSCreatePostWithTopicId:(NSString *)topicId Body:(NSString *)body completionHandle:(void (^)(id, NSError *))completionHandle
{
    NSDictionary *params = @{
                             @"body":body?:@"",
                             @"topicId":topicId?:@"",
                             };
    
    BADataEntity *entiy = [[BADataEntity alloc]init];
    entiy.urlString = [NSString stringWithFormat:kBBSCreatePostPath,[CNChangeHostManager sharedChangeHostObject].kCNALiNewHost];
    entiy.parameters = params;
    entiy.needCache = NO;
    
    return [self ba_request_POSTWithEntity:entiy successBlock:^(id response) {
        completionHandle([CNBBSCreatePostModel yy_modelWithJSON:response],nil);
    } failureBlock:^(NSError *error) {
        completionHandle(nil,error);
    } progressBlock:nil];
}

/**
 论坛引用数据
 
 @param postId 回帖id
 
 @return 网络请求任务
 */
+(id)getBBSQuoteWithPostId:(NSString *)postId completionHandle:(void (^)(id, NSError *))completionHandle
{
    NSDictionary *params = @{
                             @"postId":postId?:@"",
                             };
    
    BADataEntity *entiy = [[BADataEntity alloc]init];
    entiy.urlString = [NSString stringWithFormat:kBBSQuotePath,[CNChangeHostManager sharedChangeHostObject].kCNALiNewHost];
    entiy.parameters = params;
    entiy.needCache = NO;
    
    return [self ba_request_GETWithEntity:entiy successBlock:^(id response) {
        completionHandle([CNBBSQuoteModel yy_modelWithJSON:response],nil);
    } failureBlock:^(NSError *error) {
        completionHandle(nil,error);
    } progressBlock:nil];
}

/**
 点赞论坛
 
 @param topicId 论坛id
 
 @param postId 回帖id
 
 @return 网络请求任务
 */
+(id)getBBSDiggWithTopicId:(NSString *)topicId PostId:(NSString *)postId IsTopic:(BOOL)isTopic completionHandle:(void (^)(id, NSError *))completionHandle
{
    NSMutableDictionary *params = [[NSMutableDictionary alloc]initWithDictionary:
                                   @{
                                     @"topicId":topicId,
                                     }];
    if (!isTopic)
    {
        [params setObject:postId?:@"" forKey:@"postId"];
    }
    
    BADataEntity *entiy = [[BADataEntity alloc]init];
    entiy.urlString = [NSString stringWithFormat:kBBSDiggPath,[CNChangeHostManager sharedChangeHostObject].kCNALiNewHost];
    entiy.parameters = params;
    entiy.needCache = NO;
    
    return [self ba_request_POSTWithEntity:entiy successBlock:^(id response) {
        completionHandle([CNBBSFavoriteModel yy_modelWithJSON:response],nil);
    } failureBlock:^(NSError *error) {
        completionHandle(nil,error);
    } progressBlock:nil];
}

/**
 论坛举报
 
 @param topicId 论坛id
 
 @param postId 回帖id
 
 @return 网络请求任务
 */
+(id)getBBSReportWithTopicId:(NSString *)topicId PostId:(NSString *)postId IsTopic:(BOOL)isTopic ReasonType:(NSString *)reasonType completionHandle:(void (^)(id, NSError *))completionHandle
{
    NSMutableDictionary *params = [[NSMutableDictionary alloc]initWithDictionary:
                                   @{
                                     @"userName":[CSDNUser currentUser].username?:@"", 
                                     @"topicId":topicId,
                                     @"reasonType":reasonType?:@"",
                                     }];
    if (!isTopic)
    {
        [params setObject:postId?:@"" forKey:@"postId"];
    }
    
    BADataEntity *entiy = [[BADataEntity alloc]init];
    entiy.urlString = [NSString stringWithFormat:KBBSReportPath,[CNChangeHostManager sharedChangeHostObject].kCNALiNewHost];
    entiy.parameters = params;
    entiy.needCache = NO;
    
    return [self ba_request_POSTWithEntity:entiy successBlock:^(id response) {
        completionHandle([CNBBSReportModel yy_modelWithJSON:response],nil);
    } failureBlock:^(NSError *error) {
        completionHandle(nil,error);
    } progressBlock:nil];
}

/**
论坛任务数

@return 网络请求任务
*/
+(id)getBBSTaskCountCompletionHandle:(void(^)(id model, NSError *error))completionHandle
{
    BADataEntity *entiy = [[BADataEntity alloc]init];
    entiy.urlString = [NSString stringWithFormat:kBBSTaskCountPath,[CNChangeHostManager sharedChangeHostObject].kCNALiNewHost];
    entiy.needCache = NO;
    
    return [self ba_request_GETWithEntity:entiy successBlock:^(id response) {
        completionHandle([CNBBSPostedModel yy_modelWithJSON:response],nil);
    } failureBlock:^(NSError *error) {
        completionHandle(nil,error);
    } progressBlock:nil];
}

/**
获取字典服务

@param key 字典服务key
@return 网络请求
*/
+(id)getCsdnSelectKVWithKey:(NSString *)key kCompletionHandle{
    NSDictionary *params = @{
                             @"keys":key?:@"",
                             };
    BADataEntity *entiy = [[BADataEntity alloc]init];
    entiy.urlString = [NSString stringWithFormat:kCsdnSelectKVSPath,[CNChangeHostManager sharedChangeHostObject].kCNALiNewHost];
    entiy.parameters = params;
    entiy.needCache = NO;
    return [self ba_request_GETWithEntity:entiy successBlock:^(id response) {
        completionHandle([CNBBSApolloModel yy_modelWithJSON:response],nil);
    } failureBlock:^(NSError *error) {
        completionHandle(nil,error);
    } progressBlock:nil];
}


/**
 获取文章或者论坛是否收藏状态
 
 @param url 文章地址
 
 @return 网络请求任务
 */
+(id)getCheckCollectionWithUrl:(NSString *)url completionHandle:(void (^)(id, NSError *))completionHandle{
    NSMutableDictionary *params = [[NSMutableDictionary alloc]initWithDictionary:
                                   @{
                                     @"url":url?:@"",
                                     @"username":[CSDNUser currentUser].username?:@"",
                                     }];
    BADataEntity *entiy = [[BADataEntity alloc]init];
    entiy.urlString = [NSString stringWithFormat:kCheckCollectionPath,[CNChangeHostManager sharedChangeHostObject].kCNALiNewHost];
    entiy.parameters = params;
    entiy.needCache = NO;
    
    return [self ba_request_GETWithEntity:entiy successBlock:^(id response) {
        completionHandle([CNFavModel yy_modelWithJSON:response],nil);
    } failureBlock:^(NSError *error) {
        completionHandle(nil,error);
    } progressBlock:nil];
}
@end
